<template>
  <div class="login_con">
    <el-row>
      <el-col
        :span="12"
        :xs="0"
      ></el-col>
      <el-col
        :span="12"
        :xs="24"
      >
        <el-form class="login_form">
          <h1>hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item>
            <el-input
              placeholder="请输入用户名"
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
            <el-input
              placeholder="请输入密码"
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login_btn"
              size="default"
              @click="handleLogin"
              :loading="isLoading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
// 引入element-plus图标
import { User, Lock, View } from '@element-plus/icons-vue'
import { reactive, ref } from '@vue/reactivity'
// 引入用户小仓库
import useUserStore from '@/store/modules/user.ts'
import router from '@/router'
import { ElNotification } from 'element-plus'
// 引入获取当前时间的函数
import getTime from '@/utils/time'
const useStoer = useUserStore()
// 收集账号和密码
const loginForm = reactive({
  username: 'admin',
  password: '111111',
})
//控制登录按钮时候处于加载状态
const isLoading = ref(false)
// 点击登录按钮
const handleLogin = () => {
  isLoading.value = true // 点击登录按钮之后开始加载
  // 调用仓库中用户登录的方法
  useStoer
    .userLogin(loginForm)
    .then(() => {
      // 登录成功之后跳转至首页
      router.replace('/')
      // 登录成功提示信息
      ElNotification({
        title: `HI，${getTime()}好`,
        message: '欢迎回来',
        type: 'success',
      })
    })
    .catch((err: any) => {
      // 登录失败
      ElNotification.error(err.message)
    })
    .finally(() => {
      isLoading.value = false
    })
}

</script>
<style scoped lang="scss">
.login_con {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login_form {
  padding: 40px;
  position: relative;
  width: 80%;
  top: 30vh;
  background: url('@/assets/images/login_form.png') no-repeat;
  background-size: cover;

  .el-input {
    margin-top: 10px;
  }

  h1 {
    font-size: 40px;
    color: #fff;
  }

  h2 {
    margin: 20px 0;
    font-size: 20px;
    color: #fff;
  }

  .login_btn {
    width: 100%;
  }
}
</style>
